﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using MiruPublic;

namespace MangaKurosagi
{
    public class Chap
    {
        public bool Update = false;
        public string Url
        {
            set;
            get;
        }
        public string Title
        {
            set;
            get;
        }
        public int index
        {
            set;
            get;
        }
        public static Chap Convert(ItemChap item)
        {
            return new Chap() { Title = item.Title, Url = item.Url, index = item.index, Update = true };
        }
        public static ItemChap Convert(Chap item)
        {
            return new ItemChap { index = item.index, Title = item.Title, Url = item.Url };
        }
        public event EventHandler<GetContentCompletedEventArgs> GetContentCompleted;

        protected virtual void OnGetContentCompleted(GetContentCompletedEventArgs e)
        {
            EventHandler<GetContentCompletedEventArgs> handler = GetContentCompleted;
            if (handler != null)
            {
                handler(this, e);
            }
        }
        public string GetHtmlContent()
        {
            Website client = new Website();

            client.DownLoadURL(Global.ChapSelected.Url);

            client.DownLoadCompleted += (s, args) =>
            {
                GetContentCompletedEventArgs ar = new GetContentCompletedEventArgs();
                ar.Content=BuildContent(Utils.GetStringBetweenText(args.Result, "<div class=\"noidungchap\">", "</div>"));
                OnGetContentCompleted(ar);
            };
            return "";
        }
        private string BuildContent(string content)
        {
            string htmlText = "<html>";
            string CheckContent = content;
            string line = "";
            line = Utils.GetStringBetweenText(CheckContent, "<img src=\"", "\"");
            while (line != "")
            {

                htmlText += "<br><img src='" + line + "' width='100%'><br>";

                CheckContent = Utils.GetStringBetweenText(CheckContent, line, "");

                line = Utils.GetStringBetweenText(CheckContent, "<img src=\"", "\"");
            }
            return htmlText;
        }
    }
    public class GetContentCompletedEventArgs : EventArgs
    {
        public string Content;
    }
}
